
<template>
  <div class="tags_list">
    <el-tabs
        style="width: 100%"
        v-model="tabsValue"
        type="card"
        class="demo-tabs"
        @tab-remove="(val)=> {tabsStore.closeCurrent(val)}"
        @tab-change="handleTabClick"
    >
      <el-tab-pane
          v-for="item in tabsStore.tabList"
          :key="item.path"
          :label="item.meta.title"
          :name="item.path"
          :closable="!item.meta.affix"
      ></el-tab-pane>
    </el-tabs>

  </div>

  <div class="tags_tools">
    <el-icon class="tag_tools_icon" @click="appStore.reloadPage"><Refresh/></el-icon>

    <el-dropdown placement="top-start">
      <el-icon class="tag_tools_icon"><Setting/></el-icon>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="tabsStore.closeCurrent(route.path)">关闭当前</el-dropdown-item>
          <el-dropdown-item @click="tabsStore.closeRight(route.path)">关闭左侧</el-dropdown-item>
          <el-dropdown-item @click="tabsStore.closeOther(route.path)">关闭其他</el-dropdown-item>
          <el-dropdown-item @click="tabsStore.closeAll">关闭全部</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>

  </div>
</template>

<script setup>
import {useTabsStore, useAppStore} from '@/stores'

defineOptions({ name: 'Tabs' })

const router = useRouter()
const route = useRoute()

const tabsValue = ref(route.fullPath)
const tabsStore = useTabsStore()
const appStore = useAppStore()
tabsStore.init()

// 点击页签
const handleTabClick = (key) => {
  const obj = tabsStore.tabList.find((i) => i.path === key)
  obj ? router.push(obj.fullPath || obj.path) : router.push(key)
}

// 监听路由变化
watch(
    () => route.fullPath,
    () => {
      handleRouteChange()
      tabsValue.value = route.fullPath
    }
)

// 路由发生改变触发
function handleRouteChange() {
  const item = { ...route }
  tabsStore.addTabItem(toRaw(item))
  tabsStore.addCacheItem(toRaw(item))
}
handleRouteChange()

const handleRefresh = () => {
  router.getRoutes()
}
</script>

<style scoped lang="scss">
.tags_list {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  overflow-x: auto;
  flex-shrink: 0;
  min-width: 0;
  width: 0;
  overflow-y: hidden;

  ::v-deep .el-tabs {
    .el-tabs__header {
      margin: 0;
      border-bottom: 0;
      height: 32px;
    }

    .el-tabs__nav-next, .el-tabs__nav-prev {
      color: #303133;
      font-size: 18px;
    }
  }

}

.tags_tools {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  width: 56px;
  justify-content: flex-end;
}

.tag_tools_icon {
  font-size: 18px;
  cursor: pointer;
}

.tag_tools_icon:hover {
  background-color: #F2F3F5;
  border-radius: 10px;
}


</style>
